int len(struct TreeNode* root)
{
    if (root == NULL)
    {
        return 0;
    }
    if (root->left == root->right)
    {
        return 1;
    }
    int n = len(root->left);
    int m = len(root->right);
    return n > m ? n + 1 : m + 1;
}

void get(struct TreeNode* root, int i, double* a, int* b)
{
    if (root == NULL)
    {
        return;
    }
    a[i] += root->val;
    b[i] = b[i] + 1;
    get(root->left, i + 1, a, b);
    get(root->right, i + 1, a, b);
}

double* averageOfLevels(struct TreeNode* root, int* returnSize) {
    int length = len(root);
    *returnSize = length;
    double* a = (double*)malloc(sizeof(double) * length);
    int* b = (int*)malloc(sizeof(int) * length);
    for (int i = 0; i < length; i++)
    {
        a[i] = 0.0;
        b[i] = 0;
    }
    get(root, 0, a, b);
    for (int i = 0; i < length; i++)
    {
        a[i] = a[i] / b[i];
    }

    return a;
}